//
//  JPCustomRefreshGifHeader.m
//  JPRefreshDemo
//
//  Created by Carpenter on 2018/2/12.
//  Copyright © 2018年 carpenter. All rights reserved.
//

#import "JPCustomRefreshGifHeader.h"
#import <SDWebImage/UIImage+GIF.h>

static CGFloat const kGifViewHeight = 50.0;

@implementation JPCustomRefreshGifHeader

#pragma mark - 重写父类的方法
- (void)prepare{
    [super prepare];
    
    NSBundle * bundle = [NSBundle bundleForClass:[JPCustomRefreshGifHeader class]];
    NSURL * url = [bundle URLForResource:@"JPRefresh" withExtension:@"bundle"];
    bundle = [NSBundle bundleWithURL:url];
 
    NSString *filePath = [bundle pathForResource:@"table_refresh_header" ofType:@"gif"];
 
    NSData  *imageData = [NSData dataWithContentsOfFile:filePath];
    
    UIImage * gifImages = [UIImage sd_animatedGIFWithData:imageData];
    
    
    [self setImages:@[gifImages] forState:MJRefreshStateRefreshing];
    
    
    [self setImages:@[gifImages] forState:MJRefreshStatePulling];
    
    
    [self setImages:@[gifImages] forState:MJRefreshStateRefreshing];
    
    //隐藏时间
    self.lastUpdatedTimeLabel.hidden = YES;
    //隐藏状态
    self.stateLabel.hidden = YES;
    
}
- (void)layoutSubviews{
    [super layoutSubviews];
    
    self.bounds = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, kGifViewHeight + 20);
    
    // 指示器 gif图的frame
    self.gifView.frame = CGRectMake(self.bounds.origin.x, (self.bounds.size.height - kGifViewHeight) * 0.5, self.bounds.size.width, kGifViewHeight);
    
    self.gifView.contentMode = UIViewContentModeScaleAspectFit;
    
}

- (void)setImages:(NSArray *)images forState:(MJRefreshState)state{
    [self setImages:images duration:images.count * 0.1 forState:state];
}

@end
